def expand(a , m):
b = []
for x in a:
c = 1
while x%m==0:
c*=m
x = x//m
if len(b)!=0 and b[-1][0]==x:
b[-1][1]+=c
else:
b.append([x,c])
return b
t = int(input())
for i in range(t):
n , m = map(int , input().split())
a = list(map(int , input().split()))
k = int(input())
b = list(map(int , input().split()))
A = []
B = []
arr1 = expand(a,m)
arr2 = expand(b,m)
if arr1==arr2:
print("YES")
else:
print("NO")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
vector<pair<int, long long>> transform(vector<int> &vec, int m) {
int n = (int) vec.size();
vector<pair<int, long long>> result;
for (int ele : vec) {
long long c = 1;
while (ele % m == 0) {
ele /= m;
c *= (1LL * m);
}
if ((int) result.size() == 0 || result.back().first != ele) {
result.push_back({ele, c});
} else {
result.back().second += c;
}
}
return result;
}
void runTestCase()
{
int n, k, m;
cin >> n >> m;
vector<int> vec1(n);
for (int &ele : vec1)
cin >> ele;
cin >> k;
vector<int> vec2(k);
for (int &ele : vec2)
cin >> ele;
vector<pair<int, long long>> result1 = transform(vec1, m);
vector<pair<int, long long>> result2 = transform(vec2, m);
if (result1 == result2)
cout << "YES";
else
cout << "NO";
cout << endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("inputf.in", "r", stdin);
freopen("outputf.in", "w", stdout);
#endif
int tc = 1;
cin >> tc;
for (int i = 1; i <= tc; i++)
runTestCase();
return 0;
}
1661B - Getting Zero | 1661A - Array Balancing |
1649B - Game of Ball Passing | 572A - Arrays |
1455A - Strange Functions | 1566B - MIN-MEX Cut |
678C - Joty and Chocolate | 1352E - Special Elements |
1520E - Arranging The Sheep | 1157E - Minimum Array |
1661D - Progressions Covering | 262A - Roma and Lucky Numbers |
1634B - Fortune Telling | 1358A - Park Lighting |
253C - Text Editor | 365B - The Fibonacci Segment |
75A - Life Without Zeros | 1519A - Red and Blue Beans |
466A - Cheap Travel | 659E - New Reform |
1385B - Restore the Permutation by Merger | 706A - Beru-taxi |
686A - Free Ice Cream | 1358D - The Best Vacation |
1620B - Triangles on a Rectangle | 999C - Alphabetic Removals |
1634C - OKEA | 1368C - Even Picture |
1505F - Math | 1473A - Replacing Elements |